package mobi.pruss.multiuser;

import java.io.File;
import java.util.Comparator;
import java.util.Locale;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

public class MyApplicationInfo extends ApplicationInfo {
	public static final Comparator<MyApplicationInfo> LabelComparator = 
		new Comparator<MyApplicationInfo>() {
		public int compare(MyApplicationInfo a, MyApplicationInfo b) {
			return a.label.compareToIgnoreCase(b.label);
		}
	};
	private String label;
	private int versionCode;
	
	String getKey() {
		return Locale.getDefault().toString() + "." + uid + "." + versionCode + "." + packageName;
	}
	
	
	public MyApplicationInfo(MyCache cache, PackageManager pm, ApplicationInfo a) {
		super(a);
		
		try {
			versionCode = (pm.getPackageInfo(packageName, 0)).versionCode;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			versionCode = 0;
		}
		
		if (cache != null) {
			String cached = cache.lookup(getKey());
			if (cached != null) {
				label = cached;
				return;
			}
		}
		
		CharSequence l = pm.getApplicationLabel((ApplicationInfo)this);
		if (l == null) {
			label = packageName;
		}
		else {			
			label = (String)l;
			if (label.equals("Angry Birds")) {
				if(packageName.startsWith("com.rovio.angrybirdsrio")) {
					label = label + " Rio";
				}
				else if (packageName.startsWith("com.rovio.angrybirdsseasons")) {
					label = label + " Seasons";
				}
			}
			if (cache != null)
				cache.add(getKey(), label);
		}
	}
	
	public String getLabel() {
		return label;
	}	
}

